package com.wswy.wzcx.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.NestedScrollView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wswy.wzcx.R;
import com.wswy.wzcx.ui.other.AndroidUtilities;
import com.wswy.wzcx.ui.other.LayoutHelper;


/**
 * Created by wangduheng26 on 2017/12/18.
 * wangduheng26@gmail.com
 */

public class ChooseBottomDialog extends BottomSheetDialog implements View.OnClickListener {
    public ChooseBottomDialog(@NonNull Context context, String... strs) {
        super(context);
        NestedScrollView scrollView = new NestedScrollView(context);
        scrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        LinearLayout container = new LinearLayout(context);
        container.setOrientation(LinearLayout.VERTICAL);
        container.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        container.setDividerDrawable(ContextCompat.getDrawable(context, R.drawable.list_divider_h));
        scrollView.addView(container, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        addItems(context, container, strs);
        setContentView(scrollView);
    }

    private void addItems(Context context, LinearLayout container, String... items) {
        int size = items.length;
        for (int i = 0; i < size; i++) {
            TextView textView = new TextView(context);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.BLACK);
            LayoutHelper.setSelectable(textView);
            textView.setText(items[i]);
            textView.setTag(i);
            textView.setOnClickListener(this);
            container.addView(textView, ViewGroup.LayoutParams.MATCH_PARENT, AndroidUtilities.dp(48));

        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null && v.getTag() instanceof Integer) {
            dismiss();
            mOnItemClickListener.onItemClick(((Integer) v.getTag()));
        }
    }

}
